package com.shiliao.app.utils;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ClipboardManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;

import com.shiliao.app.R;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;
import com.yimei.framework.util.LogUtil;
import com.yimei.framework.util.ToastUtils;

/**
 * Created by yinzhiqiang on 2017/10/11.
 */

public class ShareUtils {
    private static final String UNRegisterURL = "http://www.yimeizhishi.com";
    private static UMWeb web;
    private static UMImage image;
    private static Bitmap imageBitmap;

    public static void toSharedWX(SHARE_MEDIA media, Activity activity,
                                  String url, String title, String sharDescription, String picUrl) {
        if (!TextUtils.isEmpty(picUrl)) {
            image = new UMImage(activity, picUrl);
        } else {
            imageBitmap = BitmapFactory.decodeResource(
                    activity.getResources(), R.mipmap.ic_launcher);
            image = new UMImage(activity, imageBitmap);
        }
        if (TextUtils.isEmpty(title)) {
            title = "医美知识";
        }
        if (TextUtils.isEmpty(url)) {
            url = UNRegisterURL;
        }
        setUMWeb(title, url, image,
                sharDescription);
        if (media.equals(SHARE_MEDIA.SINA)) {
            new ShareAction(activity)
                    .setPlatform(media)
                    .setCallback(umShareListener)
                    .withMedia(web).withText(title)
                    .share();
        } else {
            new ShareAction(activity)
                    .setPlatform(media)
                    .setCallback(umShareListener)
                    .withMedia(web)
                    .share();
        }
    }

    private static void setUMWeb(String title, String url, UMImage image, String desc) {
        image.compressStyle = UMImage.CompressStyle.SCALE;
        web = new UMWeb(url);
        web.setTitle(title);
        web.setThumb(image);
        web.setDescription(desc);
    }

    @SuppressLint("NewApi")
    public static void copy(String content, Context context) {
        // 得到剪贴板管理器
        ClipboardManager cmb = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
        cmb.setText(content.trim());
    }

    private static UMShareListener umShareListener = new UMShareListener() {
        @Override
        public void onStart(SHARE_MEDIA share_media) {

        }

        @Override
        public void onResult(SHARE_MEDIA platform) {
            ToastUtils.toast("成功分享医美知识至" + getMedia(platform) + "!");
        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            if (t.toString().contains("2008")) {
                ToastUtils.toast("没有安装相关应用!");
            } else {
                ToastUtils.toast("分享医美知识至" + getMedia(platform) + "失败!");
            }
            LogUtil.e("yzq", t.getMessage());
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
            ToastUtils.toast("取消分享医美知识至" + getMedia(platform) + "!");
        }
    };

    private static String getMedia(SHARE_MEDIA platform) {
        String media = "";
        switch (platform) {
            case SINA:
                media = "新浪微博";
                break;
            case QQ:
                media = "QQ好友";
                break;
            case QZONE:
                media = "QQ空间";
                break;
            case WEIXIN:
                media = "微信好友";
                break;
            case WEIXIN_CIRCLE:
                media = "微信朋友圈";
                break;
            default:
                break;
        }
        return media;
    }


}
